package com.hi.hiparty.util;

import android.annotation.SuppressLint;
import java.io.File;
import java.io.IOException;
import java.util.Random;
import java.util.StringTokenizer;
import java.util.UUID;

@SuppressLint("UseValueOf")
public class FileUtils {
	public static final String SYS_SEPARATOR = File.separator;
	public static final String WINDOWS_PATH ="C:/conf";
	public static final String LINUX_PATH = "/home/admin/conf";
	
	public static final String ANDROID_NO_SDCARD="/data/data/hiparty";
	public static final String ANDROID_SDCARD=SYS_SEPARATOR +"sdcard"+SYS_SEPARATOR+"hiparty";
	static
	{
		mkdir(Constants.SD_CARD_IMAGE_CACHE_FOLD);
	}
	public static void init(){}
	
	public static void mkdir(String path) {
		StringTokenizer st = new StringTokenizer(path, "/");
		String path1 = st.nextToken() + "/";
		String path2 = path1;
		while (st.hasMoreTokens()) {
			path1 = st.nextToken() + "/";
			path2 += path1;
			File inbox = new File(path2);
			if (!inbox.exists())
				inbox.mkdir();
		}
	}
	
	public static boolean createFileIfExist(String path){
		try {
			return new File(path).createNewFile();
		} catch (IOException e) {
			return Boolean.FALSE;
		}

	}
	
	public static boolean isLinux() {
		if ("\\".equals(File.separator)) {
			return false;
		} 
		return true;
	}
	
	public static String getPath(){
		String path = "";
		if(isLinux()){
			path = FileUtils.LINUX_PATH;
		}else{
			path = FileUtils.WINDOWS_PATH;
		}
		mkdir(path);
		return path;
	}
	
	public static String getFileSuffix(File file){
		String fileName = file.getName();
		int lastIndexOfDot = fileName.lastIndexOf('.');
		int fileNameLength = fileName.length();
        String extension = fileName.substring(lastIndexOfDot+1, fileNameLength);
		return extension;
	}
	
	public static String getSuffix(String fileName){
		int lastIndexOfDot = fileName.lastIndexOf('.');
		int fileNameLength = fileName.length();
        String extension = fileName.substring(lastIndexOfDot+1, fileNameLength);
		return extension;
	}
	
	public static String getUserAvatar(String userid){
		String[] tmp = new String[3];
		String avatarpath = "";
		try{
			Long i = Long.valueOf(userid);
			String ins = new Long(i + 100000000).toString();
			tmp[0] = ins.substring(ins.length() - 8, ins.length() - 6);
			tmp[1] = ins.substring(ins.length() - 6, ins.length() - 4);
			avatarpath = "/avatar/" + tmp[0] + "/" + tmp[1] + "/" + userid
					+ ".jpeg";
		}catch(Exception e){
			return avatarpath;
		}
		return avatarpath;
	}
	
	/**
	 * 
	 * @param userid
	 * @param fileSuffix
	 * @return
	 */
	@SuppressLint("UseValueOf")
	public static String getUploadPicPath(String userid,String fileSuffix){
		String[] tmp = new String[3];
		//
		Long i = Long.valueOf(userid);
		String uploadPicPath = "";
		try{
			String ins = new Long(i + 100000000).toString();
			tmp[0] = ins.substring(ins.length() - 8, ins.length() - 6);
			tmp[1] = ins.substring(ins.length() - 6, ins.length() - 4);

			UUID uuid = UUID.randomUUID();
			String str = uuid.toString();
			// 去掉"-"符号
			tmp[2] = str.substring(0, 8) + str.substring(9, 13)
					+ str.substring(14, 18) + str.substring(19, 23)
					+ str.substring(24);
			uploadPicPath = "/upload/" + tmp[0] + "/" + tmp[1] + "/" + tmp[2]
					+ ((null == fileSuffix) ? ".jpeg" : "." + fileSuffix);
		}catch(Exception e){
			return uploadPicPath;
		}
		return uploadPicPath;
	}
	
	public static void main(String[] args){
		//mkdir("D:/test/test/test/test1/");
		System.out.println(getPath());
		System.out.println("ee".hashCode());
		System.out.println(getUserAvatar("12345678"));
	}
}
